/*
 * Copyright (c) 2011, Michael Bedward
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this 
 * list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.mousebender.gridfacade.vector;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.WKTReader;

import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.crs.DefaultEngineeringCRS;

import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

/**
 * Base for test classes working with feature collections with vector geometries.
 * 
 * @author michael
 */
public abstract class VectorFeaturesTestBase {

    protected static final double TOL = 1.0E-8;

    protected static final CoordinateReferenceSystem CRS = DefaultEngineeringCRS.GENERIC_2D;
    protected static final ReferencedEnvelope WORLD = new ReferencedEnvelope(0, 100, 0, 100, CRS);
    
    protected static final double CELL_WIDTH = 1.0;
    protected static final double CELL_HEIGHT = 2.0;
    
    protected static final String GEOM_NAME = "the_geom";
    protected static final String VALUE_NAME = "value";
    
    protected static final WKTReader reader = new WKTReader();

    /**
     * Creates a feature collection based on the given geometries and values.
     *
     * @param geoms feature geometries
     * @param values feature values
     *
     * @return a new feature collection
     */
    protected SimpleFeatureCollection createFeatures(Geometry[] geoms, Number[] values) {
        final Class<?> valueClass = values.getClass().getComponentType();
        final Class<?> geomClass = geoms.getClass().getComponentType();
        
        SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
        typeBuilder.setName("features");
        typeBuilder.add(PolygonFeaturesTest.GEOM_NAME, geomClass, PolygonFeaturesTest.CRS);
        typeBuilder.add(PolygonFeaturesTest.VALUE_NAME, valueClass);
        final SimpleFeatureType TYPE = typeBuilder.buildFeatureType();
        
        SimpleFeatureCollection featureCollection = new ListFeatureCollection(TYPE);
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
        
        for (int i = 0; i < geoms.length; i++) {
            featureBuilder.add(geoms[i]);
            featureBuilder.add(values[i]);
            featureCollection.add(featureBuilder.buildFeature(null));
        }
        
        return featureCollection;
    }
    
}
